.PHONY : docker log linux

tag = debug
build_os = windows
build_arch = amd64
ext = .exe

DB_NAME = hc-game
DB_USER = root
DB_PASSWORD = root
DB_HOST = localhost
GAME_KINDS := 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 117

update_mod:
	go clean -modcache
	go get -u ./...
	go mod tidy

client:
	GOOS=$(build_os) GOARCH=$(build_arch) CGO_ENABLED=0 go build -tags "$(tag)" -o  ./bin/apps-$(build_os)-$(tag)/hc-c-client$(ext) ./app/client
	cp ./_run/client*.yaml ./bin/apps-$(build_os)-$(tag)/

gw_game:
	GOOS=$(build_os) GOARCH=$(build_arch) CGO_ENABLED=0 go build -tags "$(tag)" -o ./bin/apps-$(build_os)-$(tag)/hc-s-gw-game$(ext) ./app/node/gw_game

api:
	GOOS=$(build_os) GOARCH=$(build_arch) CGO_ENABLED=0 go build -tags "$(tag)" -o ./bin/apps-$(build_os)-$(tag)/hc-s-api-open$(ext) ./app/node/api/service/open
	GOOS=$(build_os) GOARCH=$(build_arch) CGO_ENABLED=0 go build -tags "$(tag)" -o ./bin/apps-$(build_os)-$(tag)/hc-s-api-backend$(ext) ./app/node/api/service/backend

actor:
	GOOS=$(build_os) GOARCH=$(build_arch) CGO_ENABLED=0 go build -tags "$(tag)" -o ./bin/apps-$(build_os)-$(tag)/hc-s-entity-actor$(ext) ./app/node/entity_actor

center:
	GOOS=$(build_os) GOARCH=$(build_arch) CGO_ENABLED=0 go build -tags "$(tag)" -o ./bin/apps-$(build_os)-$(tag)/hc-s-center$(ext) ./app/node/center

recorder:
	GOOS=$(build_os) GOARCH=$(build_arch) CGO_ENABLED=0 go build -tags "$(tag)" -o ./bin/apps-$(build_os)-$(tag)/hc-s-recorder$(ext) ./app/node/recorder

game-%:
	GOOS=$(build_os) GOARCH=$(build_arch) CGO_ENABLED=0 go build -tags "$(tag)" -o ./bin/apps-$(build_os)-$(tag)/hc-s-game-$*$(ext) ./app/games/$*

games: $(addprefix game-,$(GAME_KINDS))

server: gw_game api actor center recorder games
	cp ./_run/config*.yaml ./bin/apps-$(build_os)-$(tag)/

test-%:
	GOOS=$(build_os) GOARCH=$(build_arch) CGO_ENABLED=0 go build -tags "$(tag)" -o ./bin/tools-$(build_os)/test-$*$(ext) ./app/games/$*/tools/test

tests:$(addprefix test-,$(GAME_KINDS))
	cp ./_run/cfg_test.yaml ./bin/tools-$(build_os)/

all: client server

debug: tag=debug
debug: all

rls: tag=rls
rls: all

linux-d: build_os=linux
linux-d: tag=debug
linux-d: ext=
linux-d: all

linux-r: build_os=linux
linux-r: tag=rls
linux-r: ext=
linux-r: all

stop_db:
	docker rm -f redis
	docker rm -f mongo
	docker rm -f mysql

db_reset:stop_db run_mysql run_mongo run_redis

run_redis:
	docker run --name redis -p 6379:6379 -itd redis

run_mongo:
	docker run --name mongo -p 27017:27017 -itd mongo

run_mysql:
	docker run --name mysql -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -itd mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

run_etcd:
	rm -rf /tmp/etcd-data.tmp && mkdir -p /tmp/etcd-data.tmp && \
	  docker run \
	  -itd \
	  -p 2379:2379 \
	  -p 2380:2380 \
	  --mount type=bind,source=/tmp/etcd-data.tmp,destination=/etcd-data \
	  --name etcd \
	  gcr.io/etcd-development/etcd:v3.5.15 \
	  /usr/local/bin/etcd \
	  --name s1 \
	  --data-dir /etcd-data \
	  --listen-client-urls http://0.0.0.0:2379 \
	  --advertise-client-urls http://0.0.0.0:2379 \
	  --listen-peer-urls http://0.0.0.0:2380 \
	  --initial-advertise-peer-urls http://0.0.0.0:2380 \
	  --initial-cluster s1=http://0.0.0.0:2380 \
	  --initial-cluster-token tkn \
	  --initial-cluster-state new \
	  --log-level info \
	  --logger zap \
	  --log-outputs stderr

protoc:
	protoc -I=./models/protos/google/protos --go_opt=module=servers --go_out=. ./models/protos/google/protos/common_protos/*.proto ./models/protos/google/protos/rpc_protos/*.proto ./models/protos/google/protos/net_protos/*.proto

go_pprof:
	go tool pprof -http=:8081 http://localhost:8080/debug/pprof/profile
	go tool pprof -http=:8081 http://localhost:8080/debug/pprof/heap
	go tool pprof -http=:8081 http://localhost:8080//debug/pprof/trace



